package com.wustl.android.hood;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Point;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewInflate;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HoodMainMapBar extends LinearLayout {
	private DbLocations locations;
	
	protected void onFinishInflate() {
		Button zoomIn = (Button)findViewById(R.id.map_zoomin);
		zoomIn.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { zoom(true); }
		});
		
		Button zoomOut = (Button)findViewById(R.id.map_zoomout);
		zoomOut.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { zoom(false); }
		});
		
		Button typeMap = (Button)findViewById(R.id.maptype_map);
		typeMap.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { mapType(MAP_TYPE.MT_MAP); }
		});
		
		Button typeSatellite = (Button)findViewById(R.id.maptype_satellite);
		typeSatellite.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { mapType(MAP_TYPE.MT_SATELLITE); }
		});
		
		Button typeTerrain = (Button)findViewById(R.id.maptype_traffic);
		typeTerrain.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { toggleTraffic(); }
		});
	}
	
	private void addLoc(Point p, int zoom, int map_type, CharSequence name) {
		locations.createLocation(name.toString(), (double)p.getLatitudeE6()/1E6, (double)p.getLongitudeE6()/1E6, 0, zoom, map_type);
	}
	
	public HoodMainMapBar(Context context, AttributeSet attrs, Map inflateParams) {
		super(context, attrs, inflateParams);
		ViewInflate vi = (ViewInflate)context.getSystemService(Context.INFLATE_SERVICE);
		vi.inflate(R.layout.main_map_bar, this, true, inflateParams);
		locations = new DbLocations(context);
		locations.open();
		
		LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		List<LocationProvider> providers = lm.getProviders();
		
		// @TODO: List up provider test
		if( providers != null ) {
			StringBuffer sb = new StringBuffer();

			ListIterator<LocationProvider> it = providers.listIterator();
			
			while( it.hasNext() ) {
				LocationProvider pr = it.next();
				sb.append( pr.getName() );
			}
			
			final TextView edit = (TextView) findViewById(R.id.new_label);
			edit.setText(sb);
			ImageView add = (ImageView)findViewById(R.id.new_button);
			add.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) {
					MapView mv = ((MapView)findViewById(R.id.map));
					int map_type = DbLocations.convertMapType( MAP_TYPE.MT_MAP );
					if( mv.isSatellite() )
						map_type = DbLocations.convertMapType( MAP_TYPE.MT_SATELLITE );
					
					CharSequence name = edit.getText();
					// User should enter name
					if( name.length() == 0 ) {
						new AlertDialog.Builder(HoodMainMapBar.this.getContext())
                        .setIcon(R.drawable.hood)
                        .setTitle("You should enter Location Label")
                        .setPositiveButton("Close", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                
                                /* User clicked Yes so do some stuff */
                            }
                        })
                        .show();
					}
					else
						addLoc(mv.getMapCenter(), mv.getZoomLevel(), map_type, edit.getText());
				}
			});
		}

		// Location of Washington University
		double lat = 38.647059;
		double lon = -90.305707;

		moveTo(lat, lon, 14);
	}
	
	public void zoom(boolean zoomIn) {
		MapView map = (MapView) findViewById(R.id.map);
		MapController mc = map.getController();

		if( zoomIn )
			mc.zoomTo( map.getZoomLevel() + 1 );
		else
			mc.zoomTo( map.getZoomLevel() - 1 );
	}
	
	public void mapType( MAP_TYPE type) {
		MapView map = (MapView) findViewById(R.id.map);
		if( type == MAP_TYPE.MT_MAP ){
			if( map.isSatellite() == true )
				map.toggleSatellite();
		}
		else if( type == MAP_TYPE.MT_SATELLITE ){
			if( map.isSatellite() == false )
				map.toggleSatellite();
		}
	}
	
	public void toggleTraffic() {
		MapView map = (MapView) findViewById(R.id.map);
		map.toggleTraffic();
	}
	
	public void moveTo(double lat, double lon, int zoom ) {
		MapView map = (MapView) findViewById(R.id.map);
		MapController mc = map.getController();
		
		com.google.android.maps.Point p = new com.google.android.maps.Point((int) (lat * 1E6), (int) (lon * 1E6));
		mc.animateTo(p);
		mc.zoomTo(zoom);
	}
}
